#include "dlist.h"

LSnode* Init(LSdatatype x)
{
	LSnode* tmp = (LSnode*)malloc(sizeof(LSnode));
	if (tmp == NULL)
	{
		perror("malloc failed !");
		exit(1);
	}
	tmp->data =x;
	tmp->prev = tmp->next = tmp;
	return tmp;
}

void LSpushBack(LSnode* head,LSdatatype x)
{
	assert(head);
	LSnode* newnode = Init(x);
	LSnode* tmp = head->prev;
	head->prev = newnode;
	newnode->next = tmp->next;
	tmp->next = newnode;
	newnode->prev = tmp;
}

void Printf(LSnode* head)
{
	assert(head);
	LSnode* pcur = head->next;
	while (pcur != head)
	{
		printf("%d -> ", pcur->data);
		pcur = pcur->next;
	}
	printf("head\n");
}

void LSpopBack(LSnode* head)
{
	assert(head);
	assert(!Empty(head));
	LSnode* tmp = head->prev;
	head->prev = tmp->prev;
	tmp->prev->next = tmp->next;
	free(tmp);
	tmp = NULL;
}

void LSpushFront(LSnode* head, LSdatatype x)
{
	assert(head);
	LSnode* newnode = Init(x);
	LSnode* tmp = head->next;
	head->next = newnode;
	newnode->next = tmp;
	newnode->prev = head;
	tmp->prev = newnode;
}

void LSpopFront(LSnode* head)
{
	assert(head);
	assert(!Empty(head));
	LSnode* tmp = head->next;
	head->next = tmp->next;
	tmp->next->prev = head;
	free(tmp);
	tmp = NULL;
}

bool Empty(LSnode* head)
{
	assert(head);
	return head->next == head ? true : false;
}